var timerID;
var beepOne;
var path;
$(document).ready(function() 
{
        $("#webcamimage").scriptcam({
                onError:onError,
                cornerRadius:0,
                onWebcamReady:onWebcamReady
        });
        beepOne = $("#beep-one")[0];
        path=$("#webcamimage").attr('class');
        timerID=setInterval(function(){getQRCode()},1000);
        $("#print").click(function() 
        {
            window.frames["bill"].focus();
            window.frames["bill"].print();
        });
});

function getQRCode()
{
    var textQR=$.scriptcam.getBarCode();
    if(textQR!='')
    {
        beepOne.play();   
        var QR= textQR.replace("http://","");
        var arrayData = QR.split("*");
        document.getElementById("bill_number").value=arrayData[0];
        document.getElementById("identy_card").value=arrayData[1];
        document.getElementById("enrollment_number").value=arrayData[2];
        document.getElementById("bill_date").value=arrayData[3];
        document.getElementById("bill_total").value=arrayData[4];
        $.post(path+"index.php/ajax_controller/getDataBillByUser",{bill_number: arrayData[0],identy_card:arrayData[1],enrollment_number:arrayData[2],bill_date:arrayData[3],bill_total:arrayData[4]}, function(data) 
        {
            if (data != '') 
            {
            $('#bill').attr('src',path+"/index.php/admin_controller/loadBill");
            } else 
            {
                alert("No fue posible cargar la informacion del usuario.");    
            }
        }, 'json');
    }
}

$(window).unload(function(){
    clearInterval(timerID); // myfunction() will never be called
});


function onError(errorId,errorMsg) 
{
        alert(errorMsg);
}			
function changeCamera() 
{
        $.scriptcam.changeCamera($('#cameraNames').val());
}

function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) 
{
        $.each(cameraNames, function(index, text) {
                $('#cameraNames').append( $('<option></option>').val(index).html(text) )
        }); 
        $('#cameraNames').val(camera);
}